<?php if (!defined('TL_ROOT')) die('You can not access this file directly!');


/**
 * This class is for online shop order process - payment.
 * params order step
 * This is the children of controller.
 * PHP5 CMS Typolight
 * Copyright is 34n.sam@gmail.com
 */
 
 /**
 * Start page
 */
session_start();

$moreIdea = new ProductMoreIdea();
$moreIdea->run();

class ProductMoreIdea extends Controller
{	
	/**
	 * This is class construct function.
	 * @params null
	 * @Return null
	 */
	public function __construct()
	{
		parent::__construct();
		require_once('system/libraries/currency.php');
		$this->obj_currency = new Currency();		
	}
	
	/** 
	 * Start page function
	 * @params null
	 * @Return null
	 */
	public function run()
	{
		$this->import('Database');
		
		echo $this->displayMoreIdea();
	}
	
	/**
	 * Generate the html of product more idea
	 * @params null
	 * @Return string
	 */
	private function displayMoreIdea()
	{
		$proId = "";
		$proCategory = "";
		
		if (isset($_GET['id']) && isset($_GET['category']) && $_GET['category'] && $_GET['id'])
		{
			$proId = trim($_GET['id']);
			$proCategory = trim($_GET['category']);
		}
		else
		{
			if (isset($_SESSION['TODAY']['catalog_discount_product']) && $_SESSION['TODAY']['catalog_discount_product'])
				$proId = $_SESSION['TODAY']['catalog_discount_product'];
		}
		
		// Get the product related information.
		$objProduct = $this->Database->prepare("SELECT * FROM product_catalog WHERE id=?") 
						->execute($proId);
		
		if ($objProduct->numRows < 1)
			return;
		
		$related_skuid = $objProduct->related_skuid;
		$arrRelated = explode(',', $related_skuid);
		$strSkuid = "";
		if ($arrRelated)
		{
			foreach ($arrRelated as $strRelated)
			{
				$strSkuid .= "'" . $strRelated . "', ";
			}
		}
		
		if (strpos($strSkuid, ', ') !== false)
			$strSkuid = substr($strSkuid, 0, strlen($strSkuid) - 2);
		
		//Get related products
		$objRelatedProducts = $this->Database->prepare("SELECT * 
														FROM product_catalog 
														WHERE skuid IN (" . $strSkuid. ") AND published='1' 
														ORDER BY tstamp DESC") 
						->execute(time());
		if ($objRelatedProducts->numRows < 1)
			return;
		
		$strHtml = "<div id=\"more_ideas\">
						<h3>MORE IDEAS</h3>
						<div class=\"out\">
							<ul id=\"mycarousel\" class=\"jcarousel jcarousel-skin-tango\">";
		
		while ($objRelatedProducts->next())
		{
			//$imagePath = "daft_files/catalog/products/" . $objRelatedProducts->skuid . "/Red/1.jpg";
			$imagePath = (file_exists("daft_files/catalog/products/" . $objRelatedProducts->skuid . "/moreidea.jpg")) ? "daft_files/catalog/products/" . $objRelatedProducts->skuid . "/moreidea.jpg" : "image.php?src=" . $objRelatedProducts->img . "&width=124&height=125";
			$strHtml .= "<li>
							<a href=\"product.html?category=" . $objRelatedProducts->category . "&id=" . $objRelatedProducts->id . "\" title=\"" . $objRelatedProducts->name . "\" class=\"pic\"><img src=\"" . $imagePath . "\" alt=\"" . $objRelatedProducts->name . "\" /></a>
							<h4>" . ((strlen($objRelatedProducts->name) > 34) ? (substr($objRelatedProducts->name, 0, 32) . "...") : $objRelatedProducts->name) . "</h4>
							<span class=\"price\">" . $this->obj_currency->change_order($objRelatedProducts->price) . "</span>
						</li>";
		}
		
				
				
		$strHtml .= "</ul>
				</div>
			</div>";
		
		return $strHtml;
	}
}
?>